package com.zhuzi.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapBuilder;
import cn.hutool.script.ScriptUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 同花顺概念
 */
@Slf4j
public class GnThs {
    static LinkedHashMap<String, String> header = Maps.newLinkedHashMap();

    {
        header.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36");
        String host = "basic.10jqka.com.cn";
        header.put("Host", host);
        header.put("Referer", "http://activity.10jqka.com.cn/");
        header.put("Upgrade-Insecure-Requests", "1");
    }

    /**
     * 获取股票概念
     *
     * @param stock 股票
     * @return {@link List}<{@link String}>
     * @throws IOException ioexception
     */
    private static List<String> singleGn(String stock) throws IOException {
        String baseUrl = "http://basic.10jqka.com.cn/%s/concept.html";
        String v = cookieV();
        header.put("Cookie", "v=" + v);
        Map<String, String> cookie = MapBuilder.<String, String>create().put("v", v).build();
        Connection.Response response = JsoupHttpUtils.get(String.format(baseUrl, stock), header, cookie, null);
        String content = new String(response.bodyAsBytes(), Charset.forName("GBK"));
        Document root = Jsoup.parse(content);
        Elements tbodyEle = root.select("#concept  table.gnContent > tbody");
        log.info("获取概念|{}->{}", stock, content);
        List<String> gnList = tbodyEle.select("tr").stream().filter(item -> !item.hasClass("extend_content"))
                .map(item -> {
                    Elements td = item.select("td");
                    if (td.size() >= 2) {
                        return td.get(1).text();
                    }
                    return StringUtils.EMPTY;
                }).filter(StringUtils::isNotBlank).collect(Collectors.toList());

        log.info("获取[{}]-[{}]", stock, gnList);
        return gnList;
    }

    public static List<String> singleGnRetry(String stock) {
        return RetryerUtils.retryCallFun(() -> singleGn(stock));
    }

    public static void main(String[] args) {
        //System.out.println(singleGnRetry("300791"));
        System.out.println(cookieV());
    }

    public static String cookieV() {
        try {
            File file = ResourceUtils.getFile("classpath:js/aes.min.js");
            String scrript = FileUtil.readString(file.getPath(), StandardCharsets.UTF_8);
            Object v1 = ScriptUtil.invoke(scrript, "v");
            return v1.toString();
        } catch (FileNotFoundException e) {
        }
        return null;
    }
}
